<?php

namespace Qy\Controller;

use Qy\Controller\QyBaseController;

/**
 * 学习中心-课程-专业
 */
class SpecialtyController extends QyBaseController {

    //专业列表
    public function lists() {
        $iUserId = $this->iUserId;
        $iCategoryId = I('cate_id', 0, 'intval');
        $this->isAbilityCheck($iCategoryId);

        $objSpecialty = M('Specialty');
        $arrWhere['cate_id'] = $iCategoryId;
        $arrWhere['status'] = 1;
        $arrSpecialtyList = $objSpecialty->field('specialty_id,specialty_name,cate_id,sort')->where($arrWhere)->order('sort ASC')->select();

        if (empty($arrSpecialtyList)) {
            $this->returnList($arrSpecialtyList);
        }
        $objOpenLock = M("OpenLock");
        foreach ($arrSpecialtyList as &$val) {
            $val['lock_status'] = 1;
//            //测评通过后，解锁下级所有
//            $openSpecialtyId = $objOpenLock->where(array('user_id' => $iUserId, 'relat_id' => $val['specialty_id'], 'type' => 3))->getField('relat_id');
//            if (!empty($openSpecialtyId)) {
//                $val['lock_status'] = 1;
//            }
//            //新用户-所属岗位初级专业（sort=1）始终开启
//            if ($val['sort'] == 1) {
//                $val['lock_status'] = 1;
//            }
        }
        unset($val);
        $this->returnList($arrSpecialtyList);
    }

    //专业下的课程列表
    public function info() {
        $iUserId = $this->iUserId;
        $iPostId = $this->iPostId;
        $iSpecialtyId = $this->getParam('specialty_id', 0, 'int+', TRUE);
        //专业信息
        $returnList = M('Specialty')->field("specialty_id,cate_id,specialty_name,specialty_img,CONCAT('" . C('ADMIN_URL') . "',top_banner) as top_banner")->where(array('specialty_id' => $iSpecialtyId))->find();
        $this->isAbilityCheck($returnList['cate_id']);

        //专业下的全部课程
        $curriculumList = M('Curriculum')->where(array('specialty_id' => $iSpecialtyId, 'status' => 1))->order('sort ASC')->select();
        if (empty($curriculumList)) {
            $this->returnList($returnList);
        }
        foreach ($curriculumList as $val) {
            $currIds[] = $val['curriculum_id'];
        }

        $AllList = M()->query("
                                SELECT
                                        curriculum_id,
                                        curriculum_img,
                                        curriculum_name,
                                        description,
                                        sort,
                                        (
                                                SELECT
                                                        count(period_id)
                                                FROM
                                                        qy_period
                                                WHERE
                                                        qy_period.curriculum_id = qy_curriculum.curriculum_id AND qy_period.`status` = 1
                                        ) as period_count
                                FROM
                                        qy_curriculum
                                WHERE
                                        curriculum_id IN (" . implode(',', $currIds) . ")
                            ");

        $openLockCurrList = array();
        $openLockCurrList = M("OpenLock")->field('relat_id,status')->where(array('user_id' => $iUserId, 'type' => 2, 'relat_id' => array('in', $currIds)))->select();

        $objChapterModel = M('Chapter');
        $arrLockStatus = array();   //课程列表所有状态
        foreach ($AllList as &$val) {
            $val['chapter_count'] = $objChapterModel->where("curriculum_id ={$val['curriculum_id']} AND status=1")->getField('COUNT(*) as chapter_count');
            $val['curriculum_img'] = C('ADMIN_URL') . $val['curriculum_img'];
            foreach ($openLockCurrList as $vv) {
                if ($vv['relat_id'] == $val['curriculum_id']) {
                    $val['is_complete'] = $vv['status'] == 2 ? 1 : 0;
                }
            }
        }
        unset($val);
        $returnList['curriculum_list'] = $AllList;
        $this->returnList($returnList);
    }

    /**
     * 检查是否已测评
     * @param int $iGetPostId 当前进入的岗位ID
     */
    private function isAbilityCheck($iGetPostId) {
        return TRUE;
//        $arrPostIds = $this->authPostIds();
//        //岗位是否匹配
//        if (empty($iGetPostId) || !in_array($iGetPostId, $arrPostIds)) {
//            $this->error('该岗位无权限', -102);
//        }
//
//        //是否已测评
//        $where['user_id'] = $this->iUserId;
//        $where['cate_id'] = $iGetPostId;
//        $checkInfo = M('UserCheck')->where($where)->find();
//        if (empty($checkInfo)) {
//            $this->error("未测评");
//        }
    }

}
